<!-- 按钮一键登录 -->
<template>
	<view class="pd50">
		<view class="mylogin-img u-f-ac">
			<image src="https://sbb-sbb.oss-cn-shenzhen.aliyuncs.com/static/images/my-img/YJG_my_dp_pic_x3.png"></image>
		</view>
		<view class="u-f-ac u-fc login-box">
			
			<view class="setPayPw">
				<button class="loginWeChatBtn" open-type="getUserInfo" @getuserinfo="wechatLogin" v-if="show">授权登录</button>
				<button class="loginWeChatBtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-else>微信授权手机号码登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	
	export default{
		data() {
			return{
				show: false,
				leaderId: '',
				goIndex: false
			}
		},
		onLoad(options) {
			// this.wcLogin()
		},
		methods:{
			// 小程序进来登录
			wcLogin() {
				var that=this
				// 获取用户名  获取性别 获取头像 获取js_code去换取后台返回的openID
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
						let js_code=loginRes.code;
						// 这里根据code，传给后台，根据openId判断是否存在手机号码，存在则hasPhone = true,不存在则hasPhone = false
						var hasPhone = false
						// 判断是否授权
						uni.getSetting({
							success(res) {
								if (!res.authSetting['scope.userInfo']) {
									//这里调用授权
									that.show = true
								} else {
									// 用户已经授权过了
									if(!hasPhone) {
										// 如果不存在手机号码，则获取手机号码来注册并且登录
										that.show = false
										return
									} else {
										// 直接根据code来登录
										
									}
								}
							}
						})
					},
					fail:function(res){}
				})
			},
			// 小程序点击授权注册
			wechatLogin(e) {
				var that=this
				if(e.detail.errMsg=="getUserInfo:ok") {
					that.show = false
				} else {
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					},1000)
				}
			},
			// 退出授权
			out() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 获取手机号码
			getPhoneNumber(e) {
				var _this = this
				if(e.detail.errMsg=="getPhoneNumber:ok"){
					uni.login({
					   	provider: 'weixin',
					   	success({errMsg,code}) {
					   		if (errMsg == "login:ok") {
								uni.getUserInfo({
								    provider: 'weixin',
								    success: function (infoRes) {
									//请求后台登录
									let  params = {
										code: code,
										iv: e.detail.iv,
										encryptedData: e.detail.encryptedData,
										avatarUrl: infoRes.userInfo.avatarUrl,
										gender: infoRes.userInfo.gender,
										nickname: infoRes.userInfo.nickName,
										invitation: _this.leaderId,
									}		
									// 弹出正在登录的弹框，登录请求
									uni.showLoading({
										title: '正在登录'
									})
									appeltlogin(params).then(res =>{
										if(res.success){
											uni.setStorageSync("userInfo",res.data)
											setTimeout(function() {
												uni.hideLoading()
												_this.tui.toast('登录成功')
											}, 1000);
											if(_this.goIndex){
												setTimeout(function() {
													uni.switchTab({
														url: '../../YJG/index'
													})
												}, 2000);
											}else{
												setTimeout(function() {
													uni.navigateBack({
														delta: 1
													})
												}, 2000);
											}
										}else{
											setTimeout(function() {
												_this.tui.toast('登录失败')
												uni.hideLoading()
											}, 1000);
										}
									})
								  }
								});
								
					   		}
					   	}
					});
				}else{
				    setTimeout(function() {
				    	uni.navigateBack({
				    		delta: 1
				    	})
				    },1000) 
				}
			}
		}
	}
</script>
<style>
	page {
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
page{
	height: 100vh;
	image{
		width: 160rpx;
		height: 160rpx;
	}
}
// 
.setPayPw{
	width: 100%;
	margin-top: 80rpx;
}

.loginWeChatBtn {
	width: 100%;
	height: 88rpx;
	line-height: 88rpx;
	font-size: 30rpx;
	background-color: #F22222;
	border: none;
	color: #fff;
	margin-bottom: 40rpx;
	margin-top: 40rpx;
	border-radius: 60rpx;
}
.pd50{
	height: 100%;
}
.login-box{
	height: 100vh;
}
.mylogin-img{
	height: 50vh;
	margin: auto;
	
}
</style>

